#delimit;
set more off;
program drop _all;



use 20161021_Working_hh_1014.dta, clear;
drop if FIPS==11;
 
by FIPS,sort: egen group=rank(Y), unique;
by FIPS, sort: egen count=count(Y);
replace count=count/100;
replace group=round(group/count);

collapse totalTransfer Y count (p25) p25TT=total (p75)p75TT=total, by(State group); 
encode State, gen(state);
save 20161021_StateThumbnails.dta, replace;


	


use 20161021_State_Parameters_w_Var.dta, clear;


matrix drop _all;

sort State;
encode State, gen(state);

 
forval j=1/50 {;
mkmat A B1 B2 if state==`j', matrix(state_PAR_`j');
};


use 20161021_StateThumbnails.dta, clear;

label define state_lbl 1 "Alabama" 2 "Alaska" 3 "Arizona" 4 "Arkansas" 5 "California" 6 "Colorado" 7 "Connecticut" 8 "Delaware" 9
"Florida" 10 "Georgia" 11 "Hawaii" 12 "Idaho" 13 "Illinois" 14 "Indiana" 15 "Iowa" 16 "Kansas" 17 "Kentucky" 18 "Louisiana" 19
"Maine" 20 "Maryland" 21 "Massachusetts" 22 "Michigan" 23 "Minnesota" 24 "Mississippi" 25 "Missouri" 26 "Montana" 27 "Nebraska" 28
"Nevada" 29 "New Hampshire" 30 "New Jersey" 31 "New Mexico" 32 "New York" 33 "North Carolina" 34 "North Dakota" 35 "Ohio" 36
"Oklahoma" 37 "Oregon" 38 "Pennsylvania" 39 "Rhode Island" 40 "South Carolina" 41 "South Dakota" 42 "Tennessee" 43 "Texas" 44
"Utah" 45 "Vermont" 46 "Virginia" 47 "Washington" 48 "West Virginia" 49 "Wisconsin" 50 "Wyoming";

			
label val state state_lbl;
graph drop _all;
local i=1;
local state_label: value label state;

while `i' <= 50 {;
    local label: label `state_label' `i';

	twoway scatter total Y if state==`i' & Y<=50, m(Oh) msize(small) 
		|| rspike p75TT p25TT Y if state==`i' & Y<=50
		|| function 19.790-x, lcolor(gs2) lpattern(dash) range(0 19.790)
		|| function y= el(state_PAR_`i',1,1) + el(state_PAR_`i',1,2)*exp(el(state_PAR_`i',1,3)*x), range(0 50)
   			lwidth(thin) 
            ylabel(0(5)18, labsize(small) tlc(gs14) glc(gs14))
    		xlabel(0(10)50, labsize(small) tlc(gs14) ) 
            xscale(lc(gs14)) yscale(lc(gs14)) 
    		legend(off) graphregion(color(white))   
            graphregion(margin(t=2 b=1 l=1 r=2))
            lc(gs10)
	        text(18 40 "`label'", justification(right) size(medsmall));
       		graph save "State_`i'", replace;

       		local i = `i'+1;   
       			};


graph combine State_1.gph State_3.gph State_4.gph State_5.gph State_6.gph State_7.gph 
  State_8.gph State_10.gph State_11.gph  State_13.gph State_14.gph State_15.gph 
  State_16.gph State_17.gph State_18.gph  State_19.gph State_20.gph State_21.gph, col(3)
  b1title("Market Income ($1000s)", size(medsmall)) l1title("Social Transfers ($1000s)", size(medium)) scheme(s1mono);
         		graph display, ysize(8) xsize(6);
graph export 20161021_Appendix_1.pdf, replace fontface(Helvetica);


graph combine     State_22.gph
   State_23.gph State_24.gph State_25.gph State_26.gph State_27.gph State_28.gph State_29.gph State_30.gph 
   State_31.gph State_32.gph State_33.gph State_34.gph State_35.gph State_36.gph State_37.gph  
  State_38.gph State_39.gph, col(3)
  b1title("Market Income ($1000s)", size(medsmall)) l1title("Social Transfers ($1000s)", size(medium)) scheme(s1mono);
           		graph display, ysize(8) xsize(6);
graph export 20161021_Appendix_2.pdf, replace fontface(Helvetica);

graph combine   State_40.gph State_41.gph State_42.gph State_43.gph State_44.gph 
  State_45.gph State_46.gph State_47.gph State_48.gph State_49.gph State_50.gph, col(3)
  b1title("Market Income ($1000s)", size(medsmall)) l1title("Social Transfers ($1000s)", size(medium)) scheme(s1mono);
           		graph display, ysize(8) xsize(6);
graph export 20161021_Appendix_3.pdf, replace fontface(Helvetica);



